
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form">
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary" id="btn-expand">全部展开</button>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="btn-fold">全部折叠</button>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-primary" data-page="page/role_menu/admin_add.html"
                        data-click="common_add_click" data-callback="after_done" data-box-h="450px">新增菜单</button>
            </div>
        </div>
        <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
    </div>
</div>
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>

tb = treeTable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'auto_id',
            treePidName: 'pid',
            elem: '#munu-table',
            skin:'line',
            size:'lg',
            url: '/admin/role_menu.htm?limit=300',
            page: false,
            id:'table',
            cols: [[
                {field:'id',title:'ID',width:100},
                {field: 'title', width: 280, title: '名称'},
                {field: 'href', title: '页面',width: 260, templet:function(d){
                    return d.href === '0' ? '--' : d.href
                }},
                {field: 'icon', width: 220, align: 'center', title: '图标',templet:function (e) {
                            return e.icon ? '<i class="layui-icon '+e.icon+'"></i>' : '--'
                    }},
                {templet: '#auth-state', width: 150, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll();
            }
        });
        $('#btn-expand').click(function () {
            treeTable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treeTable.foldAll('#munu-table');
        });

function after_done(e ){
     http.get('/admin/role_menu/find_tree.htm').done(function(ret){
            if(ret.code === 200){
                const dom = layuimini.render_role_menu_select_dom(ret.result,'title');
                $(e).find('select').append(dom);
                form.render('select')
            }
            })
        }
const selected_dom = function(dom, pid){
        const new_dom = dom.split(',').filter(function (s) {
            return s && s.trim();
        });
        const res = new_dom.map(function (item) {
            return item.replace('value="' + pid.toString() + '"', 'selected="selected" value="' + pid.toString() + '"')
        });
        return res.join(',').replace(',','')
    };
//监听工具条
table.on('tool(munu-table)', function (obj) {
            const data = obj.data;
            const layEvent = obj.event;
            if (layEvent === 'del') {
                layer.confirm('确定要删除吗？',{icon:5},function(index){
                    http.get('/admin/role_menu/del.htm?id='+data.id).done(function(res)
                    {
                        if(res.code === 200){
                            obj.del()
                        }
                       layer.close(index)
                       layer.msg(res.msg)
                    })
                })
            } 
            else if (layEvent === 'edit') {
                http.getHtml('page/role_menu/admin_add.html',function(res){
                    window.openBox = layer.open({
                        type:1,
                        title:'修改权限菜单',
                        area:['600px','480px'],
                        content:res,
                       success:function(e){
                            $(e).find('legend').text('修改权限菜单')
                            http.get('/admin/role_menu/edit.htm?id='+data.id).done(function(ret){
                                if(ret.code == 200){
                                    const dom = selected_dom(layuimini.render_role_menu_select_dom(ret.result.all_data,'title'),
                                        ret.result.find_data.pid);
                                    $(e).find('select').append(dom);
                                   layuimini.render($('#add-menu'),ret.result.find_data)
                                   form.render('select')
                                }
                            })
                       }
                    })
                })
             }
        });
</script>
